package com.shaozi.nc.data.mongodao;

import java.util.List;

import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import com.shaozi.data.mongo.dao.BaseMongoDaoImpl;
import com.shaozi.nc.data.model.PushMsgModel;

@Repository("pushMsgMDao")
public class PushMsgMDao extends BaseMongoDaoImpl {

	@Autowired
	private MongoTemplate shaoziTemplate;

	public PushMsgMDao() {

	}

	public void save(String compId, PushMsgModel model) {
		shaoziTemplate.save(model, getCollectionName(compId));
	}

	public PushMsgModel getBySource(String compId, int sourceType, String uid, String sourceId) {
		Criteria c = new Criteria("sourceType").is(sourceType);
		c.and("userId").is(uid).and("sourceId").is(sourceId);
		c.and("isDel").is(0);
		Query query = new Query(c);
		return shaoziTemplate.findOne(query, PushMsgModel.class, getCollectionName(compId));
	}

	public PushMsgModel getById(String compId, String _id) {
		Criteria c = new Criteria("_id").is(new ObjectId(_id));
		c.and("isDel").is(0);
		Query query = new Query(c);
		return shaoziTemplate.findOne(query, PushMsgModel.class, getCollectionName(compId));
	}

	public List<PushMsgModel> getByCompId(String compId) {
		Criteria c = new Criteria("isDel").is(0);
		c.orOperator(Criteria.where("expireTime").is(0), Criteria.where("expireTime").gte(System.currentTimeMillis()));
		Query query = new Query(c);
		return shaoziTemplate.find(query, PushMsgModel.class, getCollectionName(compId));
	}

	/**
	 * 获取可运行的任务
	 * 
	 * @return
	 */
	public List<PushMsgModel> getRunnableJob(String compId, int limit) {
		Criteria c = new Criteria("nextPushTime").lte(System.currentTimeMillis());
		c.and("isDel").is(0);
		Query query = new Query(c);
		query.limit(limit);
		return shaoziTemplate.find(query, PushMsgModel.class, getCollectionName(compId));
	}

	public void removePushMsg(String compId, String _id) {
		Criteria c = new Criteria("_id").is(new ObjectId(_id));
		c.and("isDel").is(0);
		Query query = new Query(c);

		Update update = new Update();
		update.set("isDel", 1);
		update.set("delTime", System.currentTimeMillis());
		shaoziTemplate.updateFirst(query, update, PushMsgModel.class, getCollectionName(compId));
	}

	/**
	 * 清除已过时的任务
	 * 
	 * @param compId
	 */
	public void removeExpired(String compId) {
		Criteria c = new Criteria("expireTime").gte(System.currentTimeMillis());
		c.and("isDel").is(0);
		Query query = new Query(c);
		Update update = new Update();
		update.set("isDel", 1);
		update.set("delTime", System.currentTimeMillis());
		shaoziTemplate.updateMulti(query, update, PushMsgModel.class, getCollectionName(compId));
	}

	private String getCollectionName(String compId) {
		return String.format("%s_%s", "nc_push_msg", compId);
	}
}
